package com.chuangjiangx.member.manager.web.web.basic.controller;

import com.chuangjiangx.commons.BeanUtils;
import com.chuangjiangx.commons.response.Response;
import com.chuangjiangx.commons.response.ResponseUtils;
import com.chuangjiangx.commons.response.UnderlineResponse;
import com.chuangjiangx.member.coupon.ddd.application.MbrCardApplication;
import com.chuangjiangx.member.coupon.ddd.application.command.CreateWxCardCommand;
import com.chuangjiangx.member.coupon.ddd.application.command.UpdateWxCardCommand;
import com.chuangjiangx.member.coupon.ddd.dal.dto.MbrWxCardDTO;
import com.chuangjiangx.member.coupon.ddd.query.MbrCardQuery;
import com.chuangjiangx.member.manager.basic.web.controller.BaseController;
import com.chuangjiangx.member.manager.basic.web.interceptor.Login;
import com.chuangjiangx.member.manager.common.Constants;
import com.chuangjiangx.member.manager.common.ThreadLocalUser;
import com.chuangjiangx.member.manager.web.web.basic.request.CreateWxCardRequest;
import com.chuangjiangx.member.manager.web.web.basic.request.ModifySyncWxSwitchRequest;
import com.chuangjiangx.member.manager.web.web.basic.request.ModifyWxCardRequest;
import com.chuangjiangx.member.manager.web.web.basic.response.MbrWxCardResponse;
import com.chuangjiangx.member.manager.web.web.basic.response.SearchSyncWxStatusResponse;
import com.sun.jmx.snmp.ThreadContext;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpSession;

/**
 * 微信会员卡
 *
 * @author wp
 * @date 2018-08-28 13:49
 **/
@Slf4j
@Api(value = "MbrWxCardController", tags = "微信会员卡", description = "MbrWxCardController")
@RestController
@RequestMapping(value = "/api/mbr/wx-card-config", produces = "application/json")
public class MbrWxCardController extends BaseController {

    private MbrCardApplication mbrCardApplication;
    private MbrCardQuery mbrCardQuery;

    @Autowired
    public MbrWxCardController(MbrCardApplication mbrCardApplication, MbrCardQuery mbrCardQuery) {
        this.mbrCardApplication = mbrCardApplication;
        this.mbrCardQuery = mbrCardQuery;
    }

    /**
     * 创建微信会员卡信息
     *
     * @param session
     * @param request 创建微信会员卡信息接受参数
     * @return Response
     * @throws Exception
     */
    @PostMapping(value = "/create")
    @ApiOperation(value = "创建微信会员卡信息")
    @Login
    public Response createWxCardInfo(@ApiIgnore HttpSession session, @Validated @RequestBody CreateWxCardRequest request) throws Exception {
        ThreadLocalUser user = (ThreadLocalUser) ThreadContext.get(Constants.CURRENT_LOGIN_USER_KEY);
        Long merchantId = user.getMerchantId();
        CreateWxCardCommand command = new CreateWxCardCommand();
        BeanUtils.convertBean(request, command, (re, co) -> {
            command.setMerchantId(merchantId);
        });
        mbrCardApplication.createWxCard(command);
        return ResponseUtils.success();
    }

    /**
     * 查询微信会员卡信息
     *
     * @param session
     * @return Response
     * @throws Exception
     */
    @GetMapping(value = "/search-detail")
    @ApiOperation(value = "查询微信会员卡信息")
    @Login
    public UnderlineResponse<MbrWxCardResponse> searchDetail(@ApiIgnore HttpSession session) throws Exception {
        ThreadLocalUser user = (ThreadLocalUser) ThreadContext.get(Constants.CURRENT_LOGIN_USER_KEY);
        Long merchantId = user.getMerchantId();
        MbrWxCardResponse response = new MbrWxCardResponse();
        mbrCardApplication.syncWxStatus(merchantId);
        MbrWxCardDTO mbrWxCardDTO = mbrCardQuery.searchWxCardDetail(merchantId);
        BeanUtils.convertBean(mbrWxCardDTO,response);
        return (UnderlineResponse<MbrWxCardResponse>) ResponseUtils.success(response);
    }

    /**
     * 修改微信会员卡信息
     *
     * @param session
     * @param request 修改微信会员卡信息接受参数
     * @return Response
     * @throws Exception
     */
    @PutMapping(value = "/update")
    @ApiOperation(value = "修改微信会员卡信息")
    @Login
    public Response updataWxCard(@ApiIgnore HttpSession session, @Validated @RequestBody ModifyWxCardRequest request) throws Exception {
        UpdateWxCardCommand command = new UpdateWxCardCommand();
        BeanUtils.convertBean(request, command);
        mbrCardApplication.updateWxCard(command);
        return ResponseUtils.success();
    }

    /**
     * 查询同步至微信会员卡状态
     *
     * @param session
     * @return
     */
    @PutMapping(value = "/search-sync-wx-status")
    @ApiOperation(value = "查询同步至微信会员卡状态")
    @Login
    public Response<SearchSyncWxStatusResponse> searchSyncWxStatus(@ApiIgnore HttpSession session) {
        ThreadLocalUser user = (ThreadLocalUser) ThreadContext.get(Constants.CURRENT_LOGIN_USER_KEY);
        SearchSyncWxStatusResponse response = new SearchSyncWxStatusResponse();
        response.setWxSyncSwitch(mbrCardApplication.canSyncWx(user.getMerchantId()));
        return ResponseUtils.success(response);
    }

    /**
     * 修改微信会员卡信息
     *
     * @param session
     * @param request 修改微信会员卡信息接受参数
     * @return Response
     * @throws Exception
     */
    @PutMapping(value = "/update-sync-wx-switch")
    @ApiOperation(value = "修改微信会员卡信息")
    @Login
    public Response updateSyncWxSwitch(@ApiIgnore HttpSession session, @Validated @RequestBody ModifySyncWxSwitchRequest request) throws Exception {
        ThreadLocalUser user = (ThreadLocalUser) ThreadContext.get(Constants.CURRENT_LOGIN_USER_KEY);
        Long merchantId = user.getMerchantId();
        mbrCardApplication.updateSyncWxSwitch(merchantId,request.getWxSyncSwitch());
        return ResponseUtils.success();
    }


}
